﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.AppCode
{
    public class MoonPage
    {
        private int MinNum, MaxNum;
        public MoonPage()
        {
            NumBtnCount = 10;//默认10条
            Count = 1;//默认1页
        }
        /// <summary>
        /// 带参构造函数
        /// </summary>
        /// <param name="jsOrUrl">JS或Url地址(True为JS)</param>
        /// <param name="textType">文本类型(True为文本)</param>
        public MoonPage(bool jOrU, bool tType)
        {
            NumBtnCount = 10;//默认10条
            Count = 1;//默认1页
            JsOrUrl = jOrU;//JS或Url地址
            TextType = tType;//文本类型
        }
        /// <summary>
        /// 页总数
        /// </summary>
        public int Count { get; set; }
        /// <summary>
        /// 页索引按钮数目
        /// </summary>
        public int NumBtnCount { get; set; }
        /// <summary>
        /// JS或Url地址（主要是地址栏打印统一）。
        /// </summary>
        public bool JsOrUrl { get; set; }
        /// <summary>
        /// 文本类型（文字或标记）
        /// </summary>
        public bool TextType { get; set; }
        /// <summary>
        /// 首页图片
        /// </summary>
        public string FirstImg { get; set; }
        /// <summary>
        /// 首页灰色图片
        /// </summary>
        public string FirstImgE { get; set; }
        /// <summary>
        /// 尾页图片
        /// </summary>
        public string LastImg { get; set; }
        /// <summary>
        /// 尾页灰色图片
        /// </summary>
        public string LastImgE { get; set; }
        /// <summary>
        /// 超链接
        /// </summary>
        /// <param name="num">页码</param>
        /// <param name="css">样式</param>
        /// <param name="href">链接</param>
        /// <returns>超链接字符串，可加$num$替换页码</returns>
        private string GetA(int num, string css, string href)
        {
            StringBuilder stra = new StringBuilder();
            stra.Append("<a ");
            if (href != "")
            {
                if (JsOrUrl)
                {
                    stra.Append(string.Format("onclick=\"{0}\" title=\"第{1}页\"", href, num));
                }
                else
                {
                    stra.Append(string.Format("href=\"{0}\" title=\"第{1}页\"", href, num));
                }
                stra.Replace("$num$", num.ToString());
            }
            if (!string.IsNullOrEmpty(css))
            {
                stra.Append(string.Format(" class=\"{0}\">", css));
            }
            else
            {
                stra.Append(">");
            }
            stra.Append(num);
            stra.Append("</a>");
            return stra.ToString();
        }
        /// <summary>
        /// 文字超链接
        /// </summary>
        /// <param name="num">页码</param>
        /// <param name="css1">分页样式</param>
        /// <param name="href">链接</param>
        /// <param name="str">按钮</param>
        /// <param name="title">按钮说明</param>
        /// <returns>超链接字符串，可加$num$替换页码</returns>
        private string GetA(int num, string css, string href, string str, string title)
        {
            StringBuilder stra = new StringBuilder();
            stra.Append("<a ");
            if (href != "")
            {
                if (JsOrUrl)
                {
                    stra.Append(string.Format("onclick=\"{0}\" title=\"{1}\"", href, title));
                }
                else
                {
                    stra.Append(string.Format("href=\"{0}\" title=\"{1}\"", href, title));
                }
                stra.Replace("$num$", num.ToString());
            }
            if (css != "")
            {
                stra.Append(string.Format(" class=\"{0}\"", css));
            }
            stra.Append(">");
            stra.Append(str);
            stra.Append("</a>");
            return stra.ToString();
        }
        /// <summary>
        /// 计算显示的最小按钮和最大按钮数
        /// </summary>
        /// <param name="num">当前页</param>
        private void MathSE(int num)
        {
            int startNum = NumBtnCount / 2;
            if (Count > NumBtnCount)
            {
                if ((num - startNum) > 0 && (num + startNum) < Count)
                {
                    MinNum = num - startNum;
                    MaxNum = NumBtnCount + MinNum - 1;
                }
                else
                {
                    if ((num - startNum) <= 0)
                    {
                        MinNum = ((num - startNum) > 0) ? num - startNum : 1;
                        MaxNum = NumBtnCount + MinNum - 1;
                    }
                    if ((num + startNum) >= Count)
                    {
                        MaxNum = ((num + startNum) < Count) ? num + startNum : Count;
                        MinNum = MaxNum - NumBtnCount + 1;
                    }
                }
            }
            else
            {
                MinNum = 1;
                MaxNum = Count;
            }
        }
        /// <summary>
        /// 页码选择
        /// </summary>
        /// <param name="css">分页样式</param>
        /// <param name="href">链接</param>
        /// <returns></returns>
        private string GetFE(int num, string css, string href)
        {
            StringBuilder strfe = new StringBuilder();
            if (TextType)
            {
                if (num > 1)
                {
                    strfe.Append(GetA(num - 1, css, href, "上一页", "上一页"));
                }
                strfe.Append("$FE$");
                if (Count > num)
                {
                    strfe.Append(GetA(num + 1, css, href, "下一页", "下一页"));
                }
            }
            else
            {
                if (MinNum != 1)
                {
                    strfe.Append(GetA(1, css, href, "&laquo;", "首页"));
                }
                if (MinNum > NumBtnCount)
                {
                    strfe.Append(GetA(num - NumBtnCount, css, href, "&lsaquo;", string.Format("前{0}页", NumBtnCount)));
                }
                strfe.Append("$FE$");
                if ((Count - num) > NumBtnCount)
                {
                    strfe.Append(GetA(num + NumBtnCount, css, href, "&rsaquo;", string.Format("后{0}页", NumBtnCount)));
                }
                if (MaxNum != Count)
                {
                    strfe.Append(GetA(Count, css, href, "&raquo;", "尾页"));
                }
            }
            return strfe.ToString();
        }

        /// <summary>
        /// 分页控件
        /// </summary>
        /// <param name="num">当前页码</param>
        /// <param name="css1">分页样式</param>
        /// <param name="css2">当前选择样式</param>
        /// <param name="href">超链接字符串，可加$num$替换页码</param>
        /// <returns>分页字符串</returns>
        public string GetStrPage(int num, string css1, string css2, string href)
        {
            MathSE(num);
            StringBuilder strPage = new StringBuilder();
            strPage.Append(GetFE(num, css1, href));
            StringBuilder strNum = new StringBuilder();
            if (Count > 1)
            {
                for (int i = MinNum; i <= MaxNum; i++)
                {
                    if (i == num)
                    {
                        strNum.Append(GetA(i, css2, ""));
                    }
                    else
                    {
                        strNum.Append(GetA(i, css1, href));
                    }
                }
            }
            strPage.Replace("$FE$", strNum.ToString());
            return strPage.ToString();
        }
        private string GetFEInput(int num, string href)
        {
            StringBuilder strfe = new StringBuilder();

            if (num > 1)
            {
                strfe.Append(GetInput(num - 1, FirstImg, href, true));
            }
            else
            {
                strfe.Append(GetInput(num - 1, FirstImgE, href, false));
            }
            strfe.Append("$FE$");
            if (Count > num)
            {
                strfe.Append(GetInput(num + 1, LastImg, href, true));
            }
            else
            {
                strfe.Append(GetInput(num + 1, LastImgE, href, false));
            }
            return strfe.ToString();
        }
        /// <summary>
        /// 文字超链接
        /// </summary>
        /// <param name="num">页码</param>
        /// <param name="css1">分页样式</param>
        /// <param name="href">链接</param>
        /// <param name="str">按钮</param>
        /// <param name="title">按钮说明</param>
        /// <returns>超链接字符串，可加$num$替换页码</returns>
        private string GetInput(int num, string imgsrc, string href, bool disabled)
        {
            StringBuilder stra = new StringBuilder();
            stra.Append("<INPUT ");
            if (href != "")
            {
                stra.Append(string.Format("type=image src=\"{0}\" onclick=\"window.location.href='{1}'\" {2}"
                    , imgsrc, href, disabled ? string.Empty : "disabled"));
                stra.Replace("$num$", num.ToString());
            }
            stra.Append(">");
            return stra.ToString();
        }
        /// <summary>
        /// 分页控件
        /// </summary>
        /// <param name="num">当前页码</param>
        /// <param name="css1">分页样式</param>
        /// <param name="css2">当前选择样式</param>
        /// <param name="href">超链接字符串，可加$num$替换页码</param>
        /// <returns>分页字符串</returns>
        public string GetStrPageNews(int num, string css1, string css2, string href)
        {
            if (Count == 1)
            {
                return string.Empty;
            }
            MathSE(num);
            StringBuilder strPage = new StringBuilder();
            strPage.Append(GetFEInput(num, href));
            StringBuilder strNum = new StringBuilder();

            for (int i = MinNum; i <= MaxNum; i++)
            {
                if (i == num)
                {
                    strNum.Append(GetA(i, css2, ""));
                }
                else
                {
                    strNum.Append(GetA(i, css1, href));
                }
            }
            strPage.Replace("$FE$", strNum.ToString());
            return strPage.ToString();
        }
    }
}
